<?php

    header('Cache-Control: no-cache, no-store, must-revalidate'); // HTTP 1.1.
    header('Pragma: no-cache'); // HTTP 1.0.

    echo "<head><meta charset='UTF-16'></head>";

    include 'database.php';
    
    $userID = 1;
    
    $categoryQuery = "SELECT id, name, ownerID FROM QuizCategories WHERE ownerID=$userID ORDER BY id";
    
    $categoryResult = mysql_query($categoryQuery) or die($categoryQuery."<br/><br/>".mysql_error());
    
    echo "<link rel='stylesheet' type='text/css' href='Style.css'>";
    
    //fetch tha data from the database
    while ($category = mysql_fetch_array($categoryResult)) {
    
        echo "<div><table id='table2'><tr id='table'><td id='table'>";
    
        echo "<input type='text' id='category".$category{'id'}."' onchange='UpdateCategory(".$category{'id'}.", id)' value='".utf8_encode($category{'name'})."' maxlength='32' placeholder='Kategori'>";
    
        echo "</td><td>";
        
        echo "<input type='button' value='x' id='closeButton' onClick='DeleteCategory(".$category{'id'}.")'>";
        
        echo "</td></tr><tr><td><br></td></tr>";
    
        $categoryID = $category{'id'};
        
        $questionQuery = "SELECT id, categoryID, question, answer FROM QuizQuestions WHERE categoryID=$categoryID ORDER BY id";
        
        $questionResult = mysql_query($questionQuery) or die($questionQuery."<br/><br/>".mysql_error());
        
        $counter = 0;
        
        $number_of_questions = mysql_num_rows($questionResult);
        
        while ($question = mysql_fetch_array($questionResult)) {
        
            $counter=$counter+1;
        
            echo "<tr id='table'><td id='table'>";
        
            echo "<input type='text' id='question".$category{'id'}."_".$question['id']."_q' onchange='UpdateQuestion(".$question{'id'}.", id)' value='".utf8_encode($question{'question'})."' placeholder='Spørgsmål' maxlength='255'>";
            
            echo "</td><td id='table'>";
            
            echo "<input type='button' value='-' id='closeButton' onClick='DeleteQuestion(".$question{'id'}.")'>";
            echo "</td></tr>";
            
            echo "<tr id='table'><td id='table'>";
            
            echo "<input type='text' id='question".$category{'id'}."_".$question['id']."_a' onchange='UpdateAnswer(".$question{'id'}.", id)' value='".utf8_encode($question{'answer'})."' placeholder='Svar' maxlength='255'>";
            
            echo "</td>";
            echo "<td id='table'>";
            
            if ($counter==$number_of_questions && $counter<3) {
                echo "<input type='button' value='+' id='closeButton' onClick='CreateQuestion(".$category{'id'}.")'>";
            }
            echo "</td></tr>";
        }
        
        if ($number_of_questions==0) {
            echo "<tr><td>";
            echo "</td></tr>";
            echo "<tr><td>";
            echo "<input type='button' value='+' id='closeButton' onClick='CreateQuestion(".$category{'id'}.")'>";
            echo "</td></tr>";
        }
        
        echo "</table></div>";
        echo "<br>";
    }
    
    echo "<br>";
    echo "<center>";
     echo "<input type='button' value='Tilføj ny kategori' id='addCategory' onClick='CreateCategory(".$userID.")'>";
    echo "</center>";
    echo "<script src='UpdateFunctions6.js'></script>";
    
    
?>